/**
 * 
 */
package cn.ad.server.web.servlet.v2.parser;

import cn.ad.server.common.utils.IPUtils;
import cn.ad.server.web.servlet.v2.requests.TrackingRequest;
import cn.ad.server.web.spring.model.IAdserverRequest;
import org.apache.commons.lang3.math.NumberUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * 监测跟踪请求解析类
 * @author Wu Chengjun
 */
public abstract class TrackingRequestParser extends HttpServletParserAdapter {

	/**
	 * 请求参数名--接口版本号
	 */
	public static final String PARAM_VERSION = "version";
	/**
	 * 请求参数名--平台标识
	 */
	public static final String PARAM_PLATFORM_ID = "pid";
	
	/**
	 * 请求参数名--广告位标识
	 */
	public static final String PARAM_ADSPACE_ID = "aid";
	
	/**
	 * 请求参数名--策略标识
	 */
	public static final String PARAM_STRATEGY_ID = "sid";
	
	/**
	 * 请求参数名--素材标识
	 */
	public static final String PARAM_MATERIAL_ID = "mid";
	
	/**
	 * 请求参数名--创意标识
	 */
	public static final String PARAM_CREATIVE_ID = "cid";
	
	/**
	 * 请求参数名--广告跟踪标识
	 */
	public static final String PARAM_GUID = "guid";
	
	/**
	 * 请求参数名--位置：经度
	 */
	public static final String PARAM_LON = "lon";
	
	/**
	 * 请求参数名--位置：纬度
	 */
	public static final String PARAM_LAT = "lat";
	/**
	 * 请求参数名--设备：mac
	 */
	public static final String PARAM_MAC = "mac";
	/**
	 * 请求参数名--设备：idfa
	 */
	public static final String PARAM_IDFA = "idfa";
	/**
	 * 请求参数名--设备：imei
	 */
	public static final String PARAM_IMEI = "imei";
	
	/**
	 * 请求参数名--补量渠道：id
	 */
	public static final String PARAM_CH = "ch";
	
	/* (non-Javadoc)
	 * @see cn.falconnect.adserver.adapter.HttpServletParser#parse(javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public IAdserverRequest parse(HttpServletRequest request) throws AdRequestParseException {
		TrackingRequest adRquest = createTrackingRequest(request);
		adRquest.setIp(IPUtils.parseIp(request));
		adRquest.setUa(request.getHeader("user-agent"));
		readTrackingParam(request, adRquest);
		return adRquest;
	}
	
	/**
	 * 读取标准监测参数
	 * @param request
	 * @param trackingRequest
	 */
	public void readTrackingParam(HttpServletRequest request, TrackingRequest trackingRequest)  throws AdRequestParseException{
		if(trackingRequest == null){
			throw new NullPointerException("the argument trackingRequest is null");
		}
		trackingRequest.setVersion(request.getParameter(PARAM_VERSION));
		trackingRequest.setPlatform_id(NumberUtils.toInt(request.getParameter(PARAM_PLATFORM_ID)));
		trackingRequest.setAid(NumberUtils.toLong(request.getParameter(PARAM_ADSPACE_ID)));
		trackingRequest.setCid(NumberUtils.toLong(request.getParameter(PARAM_CREATIVE_ID)));
		trackingRequest.setMid(NumberUtils.toLong(request.getParameter(PARAM_MATERIAL_ID)));
		trackingRequest.setSid(NumberUtils.toInt(request.getParameter(PARAM_STRATEGY_ID)));
		trackingRequest.setGuid(request.getParameter(PARAM_GUID));
		trackingRequest.setLon(request.getParameter(PARAM_LON));
		trackingRequest.setLat(request.getParameter(PARAM_LAT));
		trackingRequest.setMac(request.getParameter(PARAM_MAC));
		trackingRequest.setIdfa(request.getParameter(PARAM_IDFA));
		trackingRequest.setImei(request.getParameter(PARAM_IMEI));
		trackingRequest.setCh(request.getParameter(PARAM_CH));
	}

	/**
	 * 创建一个解析监测请求实例
	 * 需要子类去实现
	 * @param request
	 * @return
	 * @throws AdRequestParseException
	 */
	public abstract TrackingRequest createTrackingRequest(HttpServletRequest request) throws AdRequestParseException;

}
